package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置服务
 */
@Service(interfaceClass = OrderSettingSerivce.class)
@Transactional
public class OrderSettingSerivceImpl implements OrderSettingSerivce {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量添加预约设置内容
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if(orderSettingList!=null && orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                //根据预约设置日期检查一下该条预约设置是否存在
                int count=orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count>0){
                    //如果该条预约设置存在。则将该条信息覆盖(即修改)
                    orderSettingDao.updateByOrderDate(orderSetting);
                }else{
                    //如果不存在，就将该条预约设置添加进去
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据月份查询当前月份预约设置信息
    @Override
    public List<Map> getOrderSettingByMonth(String date) {//2020-01
        String dateBegin=date+"-01";//2020-01-01
        String dateEnd=date+"-31";//2020-01-31
        Map<String,String> map=new HashMap<>();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
       /* List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(map);
        List<Map> mapList=new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderDateMap=new HashMap();
            orderDateMap.put("date",orderSetting.getOrderDate().getDate());
            orderDateMap.put("number",orderSetting.getNumber());
            orderDateMap.put("reservations",orderSetting.getReservations());
            mapList.add(orderDateMap);
        }*/
        List<Map> mapList = orderSettingDao.getOrderSettingByMonth(map);
        return mapList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date date = orderSetting.getOrderDate();
        //查询当前日期是否已进行预约设置
        int count = orderSettingDao.findCountByOrderDate(date);
        if(count>0){
            //已进行预约设置，则进行更新操作
            orderSettingDao.updateByOrderDate(orderSetting);
        }else{
            //没有进行预约设置，则进行插入操作
            orderSettingDao.add(orderSetting);
        }
    }

    //清理过期的预约设置
    @Override
    public void deleteOrderSettingsBeforeToday() throws Exception {
        Date date = DateUtils.getToday();
        String today = DateUtils.parseDate2String(date);
        orderSettingDao.deleteOrderSettingsBeforeToday(today);
        System.out.println("删除了"+today+"之前的预约设置");
    }
}
